package com.zq.im;

import android.content.Intent;
import android.content.MutableContextWrapper;
import android.widget.EditText;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.zq.im.api.UserApi;
import com.zq.im.bean.BaseModel;
import com.zq.im.bean.User;
import com.zq.im.retrofit.BaseCallBack;
import com.zq.im.retrofit.RetrofitManager;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Response;

public class RegisterActivity extends BaseActivity {

    private static final String TAG = "RegisterActivity";
    @BindView(R.id.account)
    EditText account;

    @BindView(R.id.password)
    EditText password;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_register;
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .statusBarDarkFont(true)
                .statusBarColor(R.color.white)
                .init();
    }

    @OnClick(R.id.register)
    void register() {

        String accountStr = account.getText().toString();
        if (accountStr.length() == 0) {
            Toast.makeText(mActivity, "请输入账号", Toast.LENGTH_SHORT).show();
            return;
        }
        String passwordStr = password.getText().toString();
        if (passwordStr.length() == 0) {
            Toast.makeText(mActivity, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        User user = new User().setPassword(passwordStr).setUsername(accountStr);

        UserApi userApi = RetrofitManager.getRetrofit().create(UserApi.class);
        Call<BaseModel<User>> call = userApi.register(user);


        call.enqueue(new BaseCallBack<User>() {
            @Override
            public void onSuccess(User user) {
                Toast.makeText(mActivity, "注册成功", Toast.LENGTH_LONG).show();
                startActivity(new Intent(mActivity, LoginActivity.class));
                finish();

            }

            @Override
            public void onFailure(Throwable t) {
                Toast.makeText(mActivity, t.getMessage(), Toast.LENGTH_LONG).show();
            }
        });


    }

}